package com.aiju.widgit.dialog;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.common.PixValue;
import com.aiju.forexpand.datepicker.OnWheelChangedListener;
import com.aiju.forexpand.datepicker.WheelView;
import com.aiju.forexpand.datepicker.WheelViewAdapter;

import java.util.Arrays;

/**
 * @author: 佐为
 * @date: 2017-05-18 17:21
 * @version: V1.0
 * @Description: 会员积分
 */
public class PointsDialog extends BaseDialog {


    private String selectItem;
    private WheelView wheelView;
    private static final String[] PLANETS = new String[]{"5", "10", "15", "20"};

    public PointsDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.LodingDialogStyle;
    }

    @Override
    protected View getView() {
        View root = mLayoutInflater.inflate(R.layout.dialog_points, null);
        root.findViewById(R.id.tv_dialog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        root.findViewById(R.id.tv_dialog_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemOnClick != null) {
                    onItemOnClick.click(selectItem, 0);
                }
                dismiss();
            }
        });
        selectItem = PLANETS[0];
        wheelView = (WheelView) root.findViewById(R.id.linear_wheel);
        wheelView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                selectItem = PLANETS[newValue];
            }
        });
        wheelView.setViewAdapter(new WheelViewAdapter() {
            @Override
            public int getItemsCount() {
                return PLANETS.length;
            }

            @Override
            public View getItem(int index, View convertView, ViewGroup parent) {
                return getTextView(PLANETS[index]);
            }

            @Override
            public View getEmptyItem(View convertView, ViewGroup parent) {
                return getTextView("");
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {

            }
        });
        wheelView.setCurrentItem(1);
        return root;
    }

    public PointsDialog setCurrentItem(String currentItem) {
        if (TextUtils.isEmpty(currentItem)) {
            wheelView.setCurrentItem(1);
            return this;
        }
        int indexOf = Arrays.asList(PLANETS).indexOf(currentItem);
        wheelView.setCurrentItem(indexOf);
        return this;
    }

    public TextView getTextView(String string) {
        TextView textView = new TextView(mContext);
        if (!TextUtils.isEmpty(string)) {
            textView.setText(string);
        }
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(new ViewGroup.LayoutParams(PixValue.dp.valueOf(60), PixValue.dp.valueOf(30)));
        textView.setBackgroundColor(Color.WHITE);
        return textView;
    }

    public interface OnItemOnClick {
        void click(String item, int position);
    }

    private OnItemOnClick onItemOnClick;

    public PointsDialog setOnItemOnClick(OnItemOnClick click) {
        this.onItemOnClick = click;
        return this;
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public PointsDialog setLayoutParams() {
        getDialogWindow().setWindowAnimations(R.style.share_animation);
        getDialogWindow().setGravity(Gravity.BOTTOM);
        getDialogWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, PixValue.dp.valueOf(213));
        return this;
    }
}
